package com.haipumi.biz.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.haipumi.beans.haipumi_admins;
import com.haipumi.biz.IBackAdminBiz;
import com.haipumi.mapper.BackAdminMapper;
import com.haipumi.utils.Encrypt;
@Service("backAdminBiz")
public class BackAdminBizImpl implements IBackAdminBiz {
	
	@Autowired
	BackAdminMapper backAdminMapper;
	
	
	@Override
	public haipumi_admins backlogin(haipumi_admins admin) {
		haipumi_admins backadmin=null;
		admin.setApwd(Encrypt.md5(admin.getApwd()));
		List<haipumi_admins>backs=backAdminMapper.backlogin(admin);
		if(backs.isEmpty()){
			return backadmin;
		}
		return backs.get(0);
	}
	@Override
	public List<haipumi_admins> findAllAdmins() {
		List<haipumi_admins>admins=backAdminMapper.findAllAdmins();
		return admins;
	}
	@Override
	public int countAllAdmins() {
		int r=backAdminMapper.countAllAdmins();
		return r;
	}
	@Override
	public haipumi_admins insertAdmin(haipumi_admins admin) {
		admin.setApwd(Encrypt.md5(admin.getApwd()));
		int r=backAdminMapper.insertAdmin(admin);
		if(r>0){
			return admin;
		}
		return null;
	}
	@Override
	public haipumi_admins editAdmin(haipumi_admins admin) {
		haipumi_admins backadmin=null;
		haipumi_admins backadmins=findAdmin(admin.getAid());
		String md5pwd=Encrypt.md5(admin.getApwd());
		String oldPwd=backadmins.getApwd().toString();
		String newPwd=admin.getApwd().toString();
		int r=0;
		if(newPwd.equals(oldPwd)){
			//两次密码相同，密码不更改
			r=backAdminMapper.editAdmin(admin);
			if(r>0){
				//修改成功
				backadmin=admin;
			}
		}else{
			admin.setApwd(md5pwd);
			r=backAdminMapper.editAdmin(admin);
			if(r>0){
				backadmin=admin;
				backadmin.setApwd(md5pwd);
			}
		}
		return backadmin;
	}
	@Override
	public haipumi_admins findAdmin(int aid) {
		return backAdminMapper.findAdmin(aid);
	}

}
